package com.zhangqi.youku;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private RelativeLayout rlLevel1,rlLevel2, rlLevel3;//3个相对布局
    //判断显示
    private boolean isLevel1Show = true;
    private boolean isLevel2Show = true;
    private boolean isLevel3Show = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView ivHome = (ImageView) findViewById(R.id.iv_home);
        ImageView ivMenu = (ImageView) findViewById(R.id.iv_menu);

        rlLevel1 = (RelativeLayout) findViewById(R.id.rl_level1);
        rlLevel2 = (RelativeLayout) findViewById(R.id.rl_level2);
        rlLevel3 = (RelativeLayout) findViewById(R.id.rl_level3);

        ivHome.setOnClickListener(this);
        ivMenu.setOnClickListener(this);
        rlLevel3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_home:
                //Toast.makeText(this,"home clicked!",Toast.LENGTH_LONG);
                Log.i("aa","home clicked!----------------------------");
                if(isLevel2Show){
                    Tools.hideView(rlLevel2);//隐藏第2层布局
                    isLevel2Show =false;
                    //在隐藏rlLevel2是判断是否隐藏了rlLevel3
                    if(isLevel3Show){
                        Tools.hideView(rlLevel3,200);//动画延迟200ms再运行
                        isLevel3Show =false;
                    }
                }else{
                    Tools.showView(rlLevel2);
                    isLevel2Show=true;
                }
                break;
            case R.id.iv_menu:
                //Toast.makeText(this,"menu clicked!",Toast.LENGTH_LONG);
                Log.i("aa", "menu clicked!----------------------------");
                if(isLevel3Show){
                    Tools.hideView(rlLevel3);
                    isLevel3Show =false;
                }else{
                    Tools.showView(rlLevel3);
                    isLevel3Show=true;
                }
                break;
            default:

                break;

        }
    }

    //设置手机home键跟我们的布局界面上的menu键功能一样
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_MENU){
            if(isLevel1Show){//此时level1在展现，我们把它隐藏,同时如果level2,3也在显示，则一并隐藏
                Tools.hideView(rlLevel1);
                isLevel1Show=false;
                if(isLevel2Show){
                    Tools.hideView(rlLevel2,200);
                    isLevel2Show = false;
                }

                if(isLevel3Show){
                    Tools.hideView(rlLevel3,300);
                    isLevel3Show = false;
                }

            }else {
                Tools.showView(rlLevel1);
                isLevel1Show = true;

                Tools.showView(rlLevel2,200);
                isLevel2Show = true;
            }
        }


        return super.onKeyDown(keyCode, event);
    }
}
